<?php
namespace Home\Org\Util;

use Home\Org\Util\ProductManager;

class UserRole
{

    /**
     * 部门：产品部:1, 市场部:2, 最高权限:3.
     * 部门内角色：专员:1, 经理:2, 总监:3. 最高权限:4.
     * 1 md5: c4ca4238a0b923820dcc509a6f75849b
     * 2 md5: c81e728d9d4c2f636f067f89cc14862c
     * 3 md5: eccbc87e4b5ce2fe28308fd9f2a7baf3
     * 4 md5: a87ff679a2f3e71d9181a67b7542122c
     */
    private $productManagerRole;
    private $salesManagerRole;
    private $roleUserModel;
    private $finalDept; // product:1, sales:2, both:3
    private $user;
    private $productManagerImpl;
    private $salesSupervisorImpl;

    public function __construct()
    {
        $this->productManagerRole = 8;
        $this->salesManagerRole = 9;
        $this->roleUserModel = M('roleUser');
        $this->finalDept = 0;
        $this->user = array();

        import("@.ORG.Util.ProductManager");
        $this->productManagerImpl = ProductManager::getInstance();
        import("@.ORG.Util.SalesSupervisor");
        $this->salesSupervisorImpl = SalesSupervisor::getInstance();

    }

    public function getProductManagerRole()
    {
        return $this->productManagerRole;
    }

    public function getSalesManagerRole()
    {
        return $this->salesManagerRole;
    }

    public function getUserDept($auth_id)
    {
        $userRoleList = $this->roleUserModel->field('role_id')->where("user_id = {$auth_id}")->select();
        foreach ($userRoleList as $key => $value)
        {// $key=0,1,2... $value = array
            //echo $value['role_id'].',';
            if( $value['role_id'] == $this->productManagerRole)
            {
                $this->finalDept += 1;
            }
            if( $value['role_id'] == $this->salesManagerRole)
            {
                $this->finalDept += 2;
            }
        }
        $this->user['dept'] = md5($this->finalDept);
        return $this->finalDept;
    }

    public function getUserRole($auth_id,$emp_no)
    {
        $emp_no = strtolower($emp_no);
        $this->getUserDept($auth_id);
        switch ($this->finalDept)
        {
            case 1:
                //echo "产品部";
                $productManagerModel = M('productManagerForReport');
                $result = $productManagerModel->field("the_role")->where("emp_no = '{$emp_no}'")->find();
                $this->user['role'] = md5($result['the_role']);
                $this->user['pmlist'] = $this->productManagerImpl->getAllList($emp_no);
                break;
            case 2:
                //echo "市场部";
                $salesManagerModel = M('salesSupervisor');
                $result = $salesManagerModel->field("the_role,Customer_Code")->where("emp_no = '{$emp_no}'")->find();
                $this->user['role'] = md5($result['the_role']);
                $this->user['Customer_Code'] = $result['Customer_Code'];
                break;
            case 3:
                //echo "最高权限";
                $this->user['role'] = md5(4);
                break;
            default:
                break;
        }
        $this->user['emp_no'] = $emp_no;
        return $this->user;
    }

    public function getPMListbyEmpNo($emp_no)
    {
        $emp_no = strtolower($emp_no);

        //echo "产品部";
        $productManagerModel = M('productManagerForReport');
        $result = $productManagerModel->field("the_role")->where("emp_no = '{$emp_no}'")->find();
        $this->user['role'] = md5($result['the_role']);
        $this->user['pmlist'] = $this->productManagerImpl->getAllList($emp_no);

        $this->user['emp_no'] = $emp_no;
        return $this->user;
    }

    public function getSSListbyEmpNo($emp_no)
    {
        $emp_no = strtolower($emp_no);

        //echo "市场部";
        $salesManagerModel = M('salesSupervisor');
        $result = $salesManagerModel->field("the_role")->where("emp_no = '{$emp_no}'")->find();
        $this->user['role'] = md5($result['the_role']);
        $this->user['sslist'] = $this->salesSupervisorImpl->getAllList($emp_no);

        $this->user['emp_no'] = $emp_no;
        return $this->user;
    }

    public function getProductManagerStaff($captain_no)
    {
        $captain_no = strtolower($captain_no);
        //echo "产品部";
        $productManagerModel = M('productManagerForReport');
        $result = $productManagerModel->field("emp_no")->where("captain_no = '{$captain_no}'")->select();
        return $result;
    }
}